Imports System
Imports System.Xml

''CONVERTED - need to add SecDefReq to get product ID for underlying Combos (pg 21)
Public Class StrikesMsg
	Inherits BaseMsg

	Protected m_startterm As String
	Protected m_endterm As String
    Protected m_strikes As New Collection
	Protected m_underProd As String
	Protected m_strikePrice As String

	'<CPCAPI v="1.1">
	'      <StrikeReq ReqID="testID" ProductID="LN" StartTerm="200706" EndTerm="200712"/>
	'</CPCAPI>

	'<CPCAPI v="1.1">
	'      <StrikeRsp ReqID="testID" ProductID="LN" StartTerm="200706" EndTerm="200712">
	'            <Strike P="1.750"/>
	'            <Strike P="2.000"/>
	'            <Strike P="2.250"/>

#Region "Properties"

    Public Property UnderlyingProduct() As String
        Get
            Return m_underProd
        End Get
        Set(ByVal value As String)
            m_underProd = value
        End Set
    End Property
	Public Property StartTerm() As String
		Get
			Return m_startterm
		End Get
		Set(ByVal Value As String)
			m_startterm = Value
		End Set
	End Property

	Public Property EndTerm() As String
		Get
			Return m_endterm
		End Get
		Set(ByVal Value As String)
			m_endterm = Value
		End Set
	End Property

	Public Property StrikePrice() As String
		Get
			Return m_strikePrice
		End Get
		Set(ByVal Value As String)
			m_strikePrice = Value
		End Set
	End Property

#End Region

	Public Sub New()
        '	RequestType = "StrikeReq"
        '	ResponseType = "StrikeRsp"
        RequestType = "DerivSecListReq"
        ResponseType = "DerivSecList"
    End Sub

	Public Function IsStrikeInCollection(ByVal strikep As String) As Boolean

		If m_strikes.Count = 0 Then
			Return False
		End If

		If Trim(strikep) = "" Then strikep = "0"

		Dim strike As Single
		Dim sstrikep As Single = Convert.ToSingle(strikep)

		For Each strike In m_strikes
			If strike = sstrikep Then
				Return True
			End If
		Next

		Return False
	End Function

	Public Function ValidateStrike(ByVal strikep As String) As Boolean

		'first, try to check individual strike price
		'submit to CME to get a return price. 
		'if the price is the same as what we want, then we are good

		If IsStrikeInCollection(strikep) Then
			Return True
		End If

		StrikePrice = strikep

		Submit()

		If IsStrikeInCollection(strikep) Then
			Return True
		End If

		StrikePrice = ""
		Submit()

		IsStrikeInCollection(strikep)
		' always return true. allow dynamic creation of strike prices. 
		Return True

    End Function

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

		If m_prodid = "" Then
			Return False
		End If

		If m_endterm = "" Then m_endterm = m_startterm

		'XmlW.WriteStartElement("Undly")
		'XmlW.WriteAttributeString("ID", m_underProd)
		'XmlW.WriteAttributeString("Src", "H")
		'XmlW.WriteAttributeString("Exch", "NYMEX")
		'XmlW.WriteAttributeString("MMY", m_startterm)

		'XmlW.WriteAttributeString("SecTyp", "FUT") ' probably should not be hard-coded, can be FUT or FWD or MLEG
		'XmlW.WriteEndElement()

		XmlW.WriteStartElement("DerivInstrmt")
		Dim sectypt As String = Globals.GetSecTyp(ProductID)
		XmlW.WriteAttributeString("SecTyp", sectypt)	' oof=option on future, ooc=option on combo
		XmlW.WriteAttributeString("Exch", "NYMEX")
		XmlW.WriteAttributeString("ID", m_prodid)
		XmlW.WriteAttributeString("Src", "H")
		XmlW.WriteAttributeString("MMY", m_startterm)
		If (StrikePrice <> "") Then
			XmlW.WriteAttributeString("StrkPx", StrikePrice)
		End If
		XmlW.WriteEndElement()


		'XmlW.WriteAttributeString("ProductID", m_prodid)
		'XmlW.WriteAttributeString("StartTerm", m_startterm)
		'XmlW.WriteAttributeString("EndTerm", m_endterm)

		Return True
	End Function

    Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

        Dim prod As XmlNode
        'Dim attr As XmlAttribute

        Dim dbi As dbInterface = Globals.dbi

        'If node.Attributes("ProductID").InnerText = m_prodid Then
        Dim sql As String

        sql = "delete from nymx_prod_strikes where prod_id=" & dbi.quote(m_prodid)
        sql &= " and first_mmy=" & dbi.quote(StartTerm)
        sql &= " and last_mmy=" & dbi.quote(EndTerm)

        dbi.nonquery(sql)

        For Each prod In node.ChildNodes
            If prod.Name = "RelSym" Then
                ' If prod.Attributes("ReqRslt").InnerText = "0" Then
                InsertStrike(prod)
                'End If
            End If
        Next


        'For Each prod In node.ChildNodes
        '    InsertStrike(prod)
        'Next
        'End If

    End Sub

    Private Sub InsertStrike(ByRef prod As XmlNode)
        '<Strike P="1.750"/>
        Dim dbi As dbInterface = Globals.dbi
        Dim price As String
        Dim instr As XmlNode

        instr = prod.ChildNodes(0)

        'If prod.Name = "Strike" Then
        price = instr.Attributes("StrkPx").InnerText
        If (instr.Attributes("PutCall").InnerText <> "1") Then
            Return
        End If

        m_strikes.Add(Convert.ToSingle(price))

        dbi.SetupStoredProc("sp_nymx_ins_product_strike")
        dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
        dbi.AddStoredProcParam("@first_mmy", SqlDbType.VarChar, StartTerm)
        dbi.AddStoredProcParam("@last_mmy", SqlDbType.VarChar, EndTerm)
        dbi.AddStoredProcParam("@strike_price", SqlDbType.VarChar, price)
        dbi.RunStoredProc()
        'End If

    End Sub
End Class
